package com.xmgl.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xmgl.dao.TestDao;
import com.xmgl.model.TestModel;

@Repository
public class TestDaoImpl implements TestDao {


	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<TestModel> findUsers() {
		// TODO Auto-generated method stub
		List<TestModel> list = (List<TestModel>) jdbcTemplate.query("select * from testTable where id < 100",new RowMapper<TestModel>(){
			
			@Override
			public TestModel mapRow(ResultSet res, int rowNum)
					throws SQLException {
				TestModel tm = new TestModel();
				tm.setId(res.getInt("id"));
				tm.setName(res.getString("name"));
				tm.setAge(res.getInt("age"));
				tm.setHobby(res.getString("hobby"));
				return tm;
			}
			
		} );
		
		return list;
	}

	@Override
	public List<TestModel> findUsers(String query) {
		// TODO Auto-generated method stub
//		Pattern pattern = Pattern.compile(query,Pattern.CASE_INSENSITIVE);
//		Criteria criteria = new Criteria("hobby").regex(pattern.toString());
//		Query q = new Query(criteria);		
//		List<TestModel> list = mongoTemplate.find("users",q, TestModel.class);
//		return list;
		return null;
	}

}
